Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
How to count number of occurrences in a very big text for each keyword (regEx) of an array [JS]

This problem is mostly related to an optimization.

Suppose I have a very big text (const text = "...") and an big array of keywords (const keywords = ["one", "good days", "ar.i.t", ...]). You can notice that this keywords might be a single word, multiple words or any RegEx expresion. I need to count how many times each keyword (expression) occurs in the text.

My approach would be as follows:

const text = "..."
const keywords = [...]

keywords.forEach(keyword => {
    const re = new RegExp(keyword, "gi");
    const count = (text.match(re) || []).length;
})

The problem with this approach is that it is very slow (if text is big enough and array as well). I have to iterate through the entire text again and again until every keyword is checked. Could someone suggest an idea of an algorithm that would go through text only once and count occurence for every keyword, or any idea that could solve the problem faster.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This might help slightly:

const str = 'this is a piece of text this is text this is text';

const countOcc = () => {
  const regex = /this/g, result, indices = [];
  while ((result = regex.exec(str))) {
    indices.push(result.index);
  }
  return indices.length
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda